import { expect } from '@jest/globals';
import path from 'path';

import { getJson5Obj } from '../../../../src/utils/json-util';
import {
  addDependency,
  addRuntimeOnly,
  hasCommonElements,
  initExecuteCommand,
  runHvigorAsync
} from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion, CaseLevel,
  LanguageType, ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: {
    [ModuleTemplateType.StaticLibrary]: 2
  }
};
const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const commandsHap = initExecuteCommand({
    sync: true,
    mode: 'module',
    module: 'entry@default',
    product: 'default',
    buildMode: 'release',
    tasks: 'assembleHap'
  });

  const entryRootPath = project.getModule('entry').moduleRootPath();
  const staticLibraryRootPath = project.getModule('staticLibrary').moduleRootPath();
  const staticLibrary1RootPath = project.getModule('staticLibrary1').moduleRootPath();

  // 添加依赖
  await addDependency(entryRootPath, { 'staticLibrary': 'file:../staticLibrary' });
  await addDependency(staticLibraryRootPath, { 'staticLibrary1': 'file:../staticLibrary1' });

  // 添加动态import配置
  await addRuntimeOnly(entryRootPath, [], ['staticLibrary']);
  await addRuntimeOnly(staticLibraryRootPath, [], ['staticLibrary1']);

  const result = await runHvigorAsync(project.projectRootPath(), commandsHap);

  // GenerateLoaderJson任务正常完成
  expect(result.stdout).toContain('Finished :entry:default@GenerateLoaderJson');

  // 检查loader.json的compileEntry
  const loaderJsonObj =
    getJson5Obj(path.resolve(entryRootPath, 'build/default/intermediates/loader/default/loader.json'));
  const compileEntry = [
    path.resolve(staticLibraryRootPath, 'index.ets'),
    path.resolve(staticLibraryRootPath, 'src/main/ets/components/mainpage/MainPage.ets'),
    path.resolve(staticLibrary1RootPath, 'index.ets'),
    path.resolve(staticLibrary1RootPath, 'src/main/ets/components/mainpage/MainPage.ets')
  ];
  expect(hasCommonElements(loaderJsonObj.compileEntry, compileEntry)).toBe(true);

  // 检查loader.json的dynamicImportLibInfo
  expect(Object.prototype.hasOwnProperty.call(loaderJsonObj.dynamicImportLibInfo, 'staticLibrary')).toBe(true);
  expect(Object.prototype.hasOwnProperty.call(loaderJsonObj.dynamicImportLibInfo, 'staticLibrary1')).toBe(true);
};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_DEVECO_STUDIO_BUILD_STAGE_DYNAMIC_IMPORT_SET_036')
  .description('1、新建工程，添加har1模块、添加har2模块\n' +
    '2、entry依赖har1，har1依赖har2\n' +
    '3、在har1/build-profile.json5.buildOptionSet.debug.arkOptions.runtimeOnly，配置packages：har2包名 \n' +
    '4、在entry/build-profile.json5.buildOptionSet.debug.arkOptions.runtimeOnly配置packages：har1包名 \n' +
    '5、product按钮中buildMode选择debug，构建hap，检查entry/build/{product}/intermediates/loader/{target}/loader.json')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L1).build())
  .testFun(testFun)
  .load();